package com.rerun.controller.college;

import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.common.util.CommonUtil;
import com.rerun.entity.EduBackground;
import com.rerun.entity.Resume;
import com.rerun.entity.SysUser;
import com.rerun.entity.vo.StudentVo;
import com.rerun.enums.DataFromEnum;
import com.rerun.service.IEduBackgroundService;
import com.rerun.service.IMemberUserService;
import com.rerun.service.IResumeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

/**
 * @author Administrator
 */

/**
 * The type Student controller.
 */
@Controller
@RequestMapping(value="/college/student")
public class StudentController extends SuperController{
	@Autowired
	private IMemberUserService memberUserService;
	@Resource
    private IEduBackgroundService iEduBackgroundService;
    @Resource
    private IResumeService iResumeService;

    /**
     * 跳转添加学生页面
     *
     * @param model the model
     * @return string string
     */
    @RequestMapping(value="toAdd")
	public String toAdd(Model model) {
		//获取当前登录用户
    	Subject subject = SecurityUtils.getSubject();
		SysUser sysUser = (SysUser) subject.getPrincipal();
		
		model.addAttribute("colegeId", sysUser.getCompanyId());
		return "business/college/addStudent";
	}

    /**
     * 添加学生信息
     *
     * @param studentVo the student vo
     * @return rest rest
     */
    @RequestMapping(value="add")
	@ResponseBody
	public Rest add(StudentVo studentVo) {
		
		 try {
			EduBackground eduBackground = new EduBackground();
			 eduBackground.setPhoneNumber(studentVo.getPhone());
			 eduBackground.setSchoolName(studentVo.getSchoolName());
			 eduBackground.setMajor(studentVo.getMajor());
			 eduBackground.setEdu(studentVo.getEdu());
			 eduBackground.setStartDate(CommonUtil.dateFormat(studentVo.getStartDate()));
			 eduBackground.setEndDate(CommonUtil.dateFormat(studentVo.getEndDate()));
			 eduBackground.setCreateTime(new Date());
			 eduBackground.setCollegeId(studentVo.getCollegeId());//院校主键关联

			 Resume resume = new Resume();
			 resume.setName(studentVo.getName());
			 resume.setSex(studentVo.getSex());
			 resume.setPhoneNumber(studentVo.getPhone());
			 resume.setBirthday(CommonUtil.dateFormat(studentVo.getBirthday()));
			 resume.setCreateTime(new Date());
			 resume.setDataFrom(DataFromEnum.COLLEGE.getType());
			 resume.setStatus(1);
			 
			 iEduBackgroundService.insert(eduBackground);
			 iResumeService.insert(resume);
		} catch (ParseException e) {
			e.printStackTrace();
			return Rest.failure("保存失败");
		}
		return Rest.ok();
	}

    /**
     * 编辑学生信息
     *
     * @param studentVo the student vo
     * @return rest rest
     */
    @RequestMapping(value="edit")
	@ResponseBody
	public Rest edit(StudentVo studentVo) {
		try {
			String phone = studentVo.getPhone();
			EduBackground background = iEduBackgroundService.selectByPhoneNumber(phone);
			Resume resume = iResumeService.selectByPhoneNumber(phone);
			
			background.setMajor(studentVo.getMajor());
			background.setEdu(studentVo.getEdu());
			background.setStartDate(CommonUtil.dateFormat(studentVo.getStartDate()));
			background.setEndDate(CommonUtil.dateFormat(studentVo.getEndDate()));
			background.setMemberId(studentVo.getId());
			
			resume.setName(studentVo.getName());
			resume.setMemberId(studentVo.getId());
			resume.setSex(studentVo.getSex());
			resume.setBirthday(CommonUtil.dateFormat(studentVo.getBirthday()));
			
			iEduBackgroundService.updateAllColumnById(background);
			iResumeService.updateAllColumnById(resume);
		} catch (Exception e) {
			e.printStackTrace();
			return Rest.failure("修改失败");
		}
		
		return Rest.ok();
	}

    /**
     * 跳转编辑学生页面
     *
     * @param phone the phone
     * @param model the model
     * @return string string
     */
    @RequestMapping(value="edit/{phone}")
	public String toEdit(@PathVariable String phone,Model model) {
		if(StringUtils.isNoneEmpty(phone)) {
			EduBackground background = iEduBackgroundService.selectByPhoneNumber(phone);
			Resume resume = iResumeService.selectByPhoneNumber(phone);
			//获取当前登录用户
	    	Subject subject = SecurityUtils.getSubject();
			SysUser sysUser = (SysUser) subject.getPrincipal();
			model.addAttribute("colegeId", sysUser.getCompanyId());
			
			StudentVo studentVo = new StudentVo();
			studentVo.setBirthday(CommonUtil.formatDate(resume.getBirthday(), "yyyy-MM-dd"));
			studentVo.setCollegeId(background.getCollegeId());
			studentVo.setSex(resume.getSex());
			studentVo.setName(resume.getName());
			studentVo.setMajor(background.getMajor());
			studentVo.setEdu(background.getEdu());
			studentVo.setStartDate(CommonUtil.formatDate(background.getStartDate(), "yyyy-MM-dd"));
			studentVo.setEndDate(CommonUtil.formatDate(background.getEndDate(), "yyyy-MM-dd"));
			studentVo.setId(resume.getMemberId());
			studentVo.setPhone(phone);
			studentVo.setSchoolName(background.getSchoolName());
			model.addAttribute("student", studentVo);
		}
		return "business/college/editStudent";
	}
}
